
<!doctype html>
<html class="no-js" lang="">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>商品列表</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="apple-touch-icon.png">
    <!-- Place favicon.ico in the root directory -->

    <link rel="stylesheet" type="text/css" href="../../css/reset.css">
</head>


<body>
    <div class="loading">
        <span class="loadImg"></span>
    </div>
    <div class="prcList">
        <div class="info">
            <ul>
                <li class="top">
                    <span class="id">id</span>
                    <span class="name">名称</span>
                    <span class="logo">logo</span>
                    <span class="prices">价格</span>
                    <span class="number">库存</span>
                    <span class="type">类型</span>
                    <span class='sale none'>是否上架</span>
                    <span class='recommend none'>是否推荐</span>
                    <span class='desc none'>商品描述</span>
                    <span class="click">操作</span>
                </li>
            </ul>
        </div>
    </div>

    <div class="revise">
        <div class="prcShow">
            <div class="info inputList">
                <p><span>商品名称:</span><input type="text" class="t1 mel" name=""></p>
                <p><span>商品logo:</span>
                    <img class="t2" src="">
                    <input type="file" name="" id="imgTest" accept=".gif,.jpg,.jpeg,.png">
                </p>
                <p><span>商品价格:</span><input type="num" class="t3 sol" name=""></p>
                <p><span>商品库存:</span><input type="num" class="t4 sol" name=""></p>
                <!-- <p><span>商品类型:</span>
                    <select class="t5">
                    </select>
                </p> -->
                <p><span>是否上下架:</span>
                    <select class="t6">
                      <option value ="1">上架</option>
                      <option value ="2">下架</option>
                    </select>
                </p>
                <p><span>是否推荐:</span>
                    <select class="t7">
                      <option value ="1">推荐</option>
                      <option value ="2">不推荐</option>
                    </select>
                </p>
                <div class="text"><span>商品描述:</span>
                    <textarea id="text" class="t8 log" ></textarea>
                </div>
                <p>
                    <button class="addClick">确定</button>
                    <button class="close">取消</button>
                </p>
            </div>
        </div>
    </div>
</body>
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../js/nicEdit.js"></script>
<script type="text/javascript">
    bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
</script>
<script type="text/javascript">
    $(function(){
        
        getData();

        //上传图片
        $('#imgTest').change(function(e){
            var reader = new FileReader();
            reader.onload = (function (file) {
                return function (e) {
                   $('.t2').attr('src',this.result);
                };
            })(e.target.files[0]);
            reader.readAsDataURL(e.target.files[0]);
        })

        //获取商品数据
        function getData(){
            var admintoken=$.cookie('admintoken');
            if(admintoken!=null){
                $('.loading').show();
                $.ajax({
                    type: 'get',
                    url:'http://www.math80.com/goods?_sort=-id',
                    headers:{Authorization:"Bearer "+admintoken},
                    success:function(data){
                        var all=data.data;
                        var len=data.data.length;
                        $('.info ul li').not('.top').remove();
                        for(var i=0;i<len;i++){
                            var li="<li>"+
                                        "<span class='id'>"+all[i].id+"</span>"+
                                        "<span class='name'>"+all[i].goods_name+"</span>"+
                                        "<span class='logo'><img src='"+all[i].goods_logo+"'></span>"+
                                        "<span class='prices'>"+all[i].goods_prices+"</span>"+
                                        "<span class='number'>"+all[i].goods_number+"</span>"+
                                        "<span class='type'>"+all[i].goods_type+"</span>"+
                                        "<span class='sale none'>"+all[i].is_sale+"</span>"+
                                        "<span class='recommend none'>"+all[i].is_recommend+"</span>"+
                                        "<span class='desc none'>"+all[i].goods_desc+"</span>"+
                                        "<span class='click'><a class='clicka'>修改</a> <a class='clickb'>删除</a></span>"+
                                    "</li>";
                            
                            $('.info ul').append(li);
                        }
                        $('.prcAdd input').val('');
                        $('.nicEdit-main').html('');
                        id=null;
                        $('.loading').hide();
                    }
                })
            }else{
                $('.login').css('display','block')
            }
        }


        //点击修改
        var id=null;
        $(document).on('click','.clicka',function(){

            id=$(this).parent().parent().find('.id').html();
            var name=$(this).parent().parent().find('.name').html();
            var logo=$(this).parent().parent().find('.logo img').attr('src');
            var prices=$(this).parent().parent().find('.prices').html();
            var number=$(this).parent().parent().find('.number').html();
            var sale=$(this).parent().parent().find('.sale').html();
            var recommend=$(this).parent().parent().find('.recommend').html();
            var desc=$(this).parent().parent().find('.desc').html();

            $('.t1').val(name);
            $('.t2').attr('src',logo);
            $('.t3').val(prices);
            $('.t4').val(number);
            $('.t6').val(sale);
            $('.t7').val(recommend);
            $('.nicEdit-main').html(desc);
        });

        //点击删除
        $(document).on('click','.clickb',function(){
            $('.loading').show();
            id=$(this).parent().parent().find('.id').html();
            var admintoken=$.cookie('admintoken');
            if(admintoken!=null&&id!=null){
                $.ajax({
                    type: 'DELETE',
                    url:'http://www.math80.com/goods/'+id,
                    headers:{Authorization:"Bearer "+admintoken},
                    success:function(data){
                        $('.loading').hide();
                        getData()
                    }
                })
            }else{
                alert("重新登录")
            }

        });

        //提交修改内容
        $('.addClick').click(function(){
            var admintoken=$.cookie('admintoken');
            if(admintoken!=null&&id!=null){
                var n_name=$('.t1').val();
                var n_logo=$('.t2').attr('src');
                var n_prices=$('.t3').val();
                //var n_sort=$('.t4').val();
                var n_number=$('.t4').val();
                var n_sale=$('.t6').val();
                var n_recommend=$('.t7').val();
                var n_desc = nicEditors.findEditor('text').getContent();

                var data={
                    goods_name:n_name,
                    goods_logo_byte:n_logo,
                    goods_prices:n_prices,
                    goods_number:n_number,
                    is_sale:n_sale,
                    is_recommend:n_recommend,
                    goods_desc:n_desc
                };
                $('.loading').show();
                $.ajax({
                    type: 'put',
                    url:'http://www.math80.com/goods/'+id,
                    headers:{Authorization:"Bearer "+admintoken},
                    data:data,
                    success:function(data){
                        alert('修改成功！');
                        $('.loading').hide();
                        getData();
                        $('.inputList input').val('');
                        $('.inputList img').attr('src','');
                        $('.nicEdit-main').html('');
                        id=null;
                    }
                })
            }else{
                alert("重新登录")
            }
        });

        //取消修改
        $('.close').click(function(){
            $('.inputList input').val('');
            $('.inputList img').attr('src','');
            $('.nicEdit-main').html('');
            id=null;
        })
        


    })

</script>
</html>